package com.kung.qvs.app;

import android.annotation.SuppressLint;
import android.app.Application;

import com.kung.qvs.data.JtRepository;
import com.kung.qvs.ui.login.LoginViewModel;
import com.kung.qvs.ui.login.SplashViewModel;
import com.kung.qvs.ui.main.HomeViewModel;
import com.kung.qvs.ui.main.HomeWorkerModel;
import com.kung.qvs.ui.main.fragment.BleDeviceViewModel;
import com.kung.qvs.ui.main.fragment.CollectTrashRecordViewModel;
import com.kung.qvs.ui.main.fragment.CompanyInfoViewModel;
import com.kung.qvs.ui.main.fragment.CustomerEditViewModel;
import com.kung.qvs.ui.main.fragment.CustomerManageViewModel;
import com.kung.qvs.ui.main.fragment.MainViewModel;
import com.kung.qvs.ui.main.fragment.PwdForgetModel;
import com.kung.qvs.ui.main.fragment.PwdResetModel;
import com.kung.qvs.ui.main.fragment.SettingViewModel;
import com.kung.qvs.ui.main.fragment.StatisticTotalModel;
import com.kung.qvs.ui.main.fragment.SupplierEditViewModel;
import com.kung.qvs.ui.main.fragment.SupplierManageViewModel;
import com.kung.qvs.ui.main.fragment.WorkloadStatisticsModel;
import com.kung.qvs.ui.main.fragment.manage.FragProductAddModel;
import com.kung.qvs.ui.main.fragment.manage.FragProductsModel;
import com.kung.qvs.ui.main.fragment.manage.FragWorkerAddModel;
import com.kung.qvs.ui.main.fragment.manage.FragWorkersModel;
import com.kung.qvs.ui.network.NetWorkViewModel;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

/**
 * Created by goldze on 2019/3/26.
 */
public class AppViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AppViewModelFactory INSTANCE;
    private final Application mApplication;
    private final JtRepository mRepository;

    public static AppViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AppViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository());
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    private AppViewModelFactory(Application application, JtRepository repository) {
        this.mApplication = application;
        this.mRepository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(NetWorkViewModel.class)) {
            return (T) new NetWorkViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            return (T) new LoginViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(HomeViewModel.class)) {
            return (T) new HomeViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MainViewModel.class)) {
            return (T) new MainViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(BleDeviceViewModel.class)) {
            return (T) new BleDeviceViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SettingViewModel.class)) {
            return (T) new SettingViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(CollectTrashRecordViewModel.class)) {
            return (T) new CollectTrashRecordViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(CompanyInfoViewModel.class)) {
            return (T) new CompanyInfoViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SplashViewModel.class)) {
            return (T) new SplashViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(PwdResetModel.class)) {
            return (T) new PwdResetModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SupplierEditViewModel.class)) {
            return (T) new SupplierEditViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SupplierManageViewModel.class)) {
            return (T) new SupplierManageViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(CustomerEditViewModel.class)) {
            return (T) new CustomerEditViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(CustomerManageViewModel.class)) {
            return (T) new CustomerManageViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(PwdForgetModel.class)) {
            return (T) new PwdForgetModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(FragProductsModel.class)) {
            return (T) new FragProductsModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(FragProductAddModel.class)) {
            return (T) new FragProductAddModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(FragWorkersModel.class)) {
            return (T) new FragWorkersModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(FragWorkerAddModel.class)) {
            return (T) new FragWorkerAddModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(WorkloadStatisticsModel.class)) {
            return (T) new WorkloadStatisticsModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(HomeWorkerModel.class)) {
            return (T) new HomeWorkerModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(StatisticTotalModel.class)) {
            return (T) new StatisticTotalModel(mApplication, mRepository);
        }
        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
